﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using SharpDiary.Core.ORMapping;
using SharpDiary.Core.Utility;
using SharpDiary.Core.Common;
using SharpDiary.Core.Controls;

namespace SharpDiary.Diary
{
    public partial class DiaryInfoListItemControl : ListItemControlBase
    {
        #region Fields & Properties

        private DiaryItemPasswordView _passwordView;

        private DiaryInfo _diary;

        public DiaryInfo Diary
        {
            get { return _diary; }
            set
            {
                _diary = value;

                if (_diary != null && !string.IsNullOrEmpty(_diary.PrivatePasswordMD5) && _passwordView == null)
                {
                    _passwordView = new DiaryItemPasswordView();
                    _passwordView.Visible = false;
                    _passwordView.Dock = DockStyle.Fill;
                    this.Controls.Add(_passwordView);
                }

                if (_passwordView != null)
                {
                    _passwordView.Hide();
                    _passwordView.Diary = value;
                }

                DisplayDiary();
            }
        }

        private int _summaryTextMaxLength = 20;

        public int SummaryTextMaxLength
        {
            get { return _summaryTextMaxLength; }
            set { _summaryTextMaxLength = value; }
        }

        /// <summary>
        /// 获取是否需要输入独立访问密码。
        /// </summary>
        public bool NeedEnterPassword
        {
            get { return (_passwordView != null && _passwordView.Visible); }
        }

        #endregion

        #region Constructors

        public DiaryInfoListItemControl()
        {
            InitializeComponent();


        }

        #endregion

        protected override void DisplayItemInfo()
        {
            base.DisplayItemInfo();

            DisplayDiary();
        }

        /// <summary>
        /// 显示日记信息。
        /// </summary>
        private void DisplayDiary()
        {
            if (_diary == null) { return; }

            //有独立密码的日记，显示输入密码提示
            if (!string.IsNullOrEmpty(_diary.PrivatePasswordMD5))
            {

                _passwordView.BringToFront();
                _passwordView.Show();
            }

            Title = _diary.Title;
            Description = Utils.GetSummaryText(_diary.Content, _summaryTextMaxLength);
            CreatedAndUpdateDatetime = string.Format("{0} {1}",
               "创建于: " + _diary.CreatedDatetime.ToShortDateString() + " " + _diary.CreatedDatetime.ToLongTimeString(),
                (_diary.CreatedDatetime == _diary.UpdateDatetime ? string.Empty : (string.Format("(修改于: {0})", _diary.UpdateDatetime.ToShortDateString() + " " + _diary.UpdateDatetime.ToLongTimeString())))
                );

            EmotionInfo emotion = EmotionInfo.GetEmotionInfoById(_diary.EmotionId);
            if (emotion != null && emotion.Picture != null && emotion.Picture.Length > 0)
            {
                ImageAboveLogo = Utils.BytesToImage(emotion.Picture);
            }
        }

        /// <summary>
        /// 编辑日记信息。
        /// </summary>
        private void EditDiaryInfo()
        {
            if (_passwordView == null || !_passwordView.Visible)
            {
                UserSettings.ShellInstance.ExecuteAction("SharpDiary.Diary", "c67c1ee9-8029-4849-8918-6cf3153497fe", _diary, this);
            }
        }

        protected override void UpdateSelectedState(bool selected)
        {
            base.UpdateSelectedState(selected);
        }

        protected override void OnEditButtonClick(object sender, EventArgs e)
        {
            base.OnEditButtonClick(sender, e);

            EditDiaryInfo();
        }

        /// <summary>
        /// 删除日记。
        /// </summary>
        private void DeleteDiary()
        {
            if (_diary == null) { return; }

            if (MessageBoxHelper.ShowConfirmation("您确定要删除此日记吗？", true) == DialogResult.Yes)
            {
                if (DiaryInfo.Remove<DiaryInfo>(_diary))
                {
                    this.Hide();
                    this.Dispose();

                    //日记被删除，通知已打开的日记编辑窗体立即关闭。
                    UserSettings.ShellInstance.NotifyMessage(this, new MessageReceivedEventArgs(MessageDefinition.CloseNow, "", "3b42c11a-18e8-476c-b6c3-87137d093c67", _diary.Id));
                }
                else
                {
                    MessageBoxHelper.ShowAlert("删除失败。");
                }
            }
        }

        protected override void OnDeleteButtonClick(object sender, EventArgs e)
        {
            base.OnDeleteButtonClick(sender, e);

            DeleteDiary();
        }

        private void lblPaperConer_Click(object sender, EventArgs e)
        {
            EditDiaryInfo();
        }
    }
}
